package com.wjw.java.thread;

/**
 * 在主线程中,控制子线程的退出
 */
public class MainExitChildrenThread {

    public static void main(String[] args) {
        T t1 = new T();
        t1.start();

        try {
            System.out.println("主线程：开始休眠10秒...");
            Thread.sleep(10000);
            System.out.println("主线程：休眠结束,停止子线程运行");
            t1.setIfLoop(false);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}

class T extends Thread{
    private int num;
    private boolean ifLoop = true;

    public boolean isIfLoop() {
        return this.ifLoop;
    }

    public void setIfLoop(boolean ifLoop) {
        this.ifLoop = ifLoop;
    }

    @Override
    public void run() {

        while (ifLoop){
            String name = Thread.currentThread().getName();
            try {
                Thread.sleep(1000);
                System.out.println("线程："+name+"正在运行中..."+ (++num));
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}